<template>
  <el-drawer v-model="addDrawer" size="50%" :show-close="false" @opened="openDrawer" @close="closeDrawer">
    <template #title>
      <h2><svg-icon icon-class="list"></svg-icon><span style="padding-left: 15px">{{ formName }}</span></h2>
      <el-button type="primary" @click="confirmAndSave" :disabled="drawerLoading" v-if="!form[idName]">保存&继续填写</el-button>
      <el-button type="primary" @click="confirmClick" :disabled="drawerLoading" v-if="!(route.path.includes('dataIntegrate') || route.path.includes('achievementSearch'))">保存</el-button>
    </template>
    <template #default>
      <el-form :model="form" ref="addRef" :inline="true" :rules="formRules" label-width="160px" v-loading="drawerLoading">
        <el-row justify="space-between">
          <el-form-item label="省" prop="provinceId" v-if="provinceData.length > 0 || form.provinceId">
            <el-select v-model="form.provinceId" placeholder="请选择省" @change="chooseFormProvince" clearable>
              <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="省" v-else>
            <el-input v-model="form.province" clearable disabled></el-input>
          </el-form-item>
          <el-form-item label="市" prop="cityId" v-if="provinceData.length > 0 || form.cityId">
            <el-select v-model="form.cityId" placeholder="请选择市" @change="chooseFormCity" clearable>
              <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="市" v-else>
            <el-input v-model="form.city" clearable disabled></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="区/县" prop="countyId" v-if="provinceData.length > 0 || form.countyId">
            <el-select v-model="form.countyId" placeholder="请选择区/县" @change="chooseFormCounty" clearable>
              <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="区/县" v-else>
            <el-input v-model="form.county" clearable disabled></el-input>
          </el-form-item>
          <el-form-item label="记录编号" prop="id">
            <el-input v-model="form.id" placeholder="请输入记录编号" clearable maxlength="17"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="探槽编号" prop="trenchId">
            <el-input v-model="form.trenchId" placeholder="请输入探槽编号" clearable maxlength="17"></el-input>
          </el-form-item>
          <el-form-item label="古地震发生时间" prop="occurDateEst">
            <el-input v-model="form.occurDateEst" placeholder="请输入古地震发生时间" clearable maxlength="8"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="古地震发生时间误差" prop="occurDateEr">
            <el-input v-model="form.occurDateEr" placeholder="请输入古地震发生时间误差" clearable maxlength="8"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="古地震发生时间区间" prop="occurDateEstInterval" style="width: 100%">
            <el-input v-model="form.occurDateEstInterval" placeholder="请输入古地震发生时间区间" clearable
              :autosize="{ minRows: 4, maxRows: 8 }" type="textarea" show-word-limit maxlength="200"
              resize="none"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="同震位移 [[米]]" prop="offsetEst">
            <el-input v-model="form.offsetEst" placeholder="请输入同震位移 [[米]]" clearable maxlength="8"></el-input>
          </el-form-item>
          <el-form-item label="同震位移误差值" prop="coordination">
            <el-input v-model="form.coordination" placeholder="请输入同震位移误差值" clearable maxlength="8"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="错动性质" prop="slipFeature">
            <dict-select code="FaultTypeCVD" :value="form.slipFeature"
              @update="val => form.slipFeature = val"></dict-select>
          </el-form-item>
          <el-form-item label="识别标志" prop="evidence">
            <el-input v-model="form.evidence" placeholder="请输入识别标志" clearable maxlength="20"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="备注" prop="commentInfo" style="width:100%">
            <el-input v-model="form.commentInfo" placeholder="请输入备注" :autosize="{ minRows: 5, maxRows: 10 }"
              type="textarea" show-word-limit maxlength="2147483647" clearable></el-input>
          </el-form-item>
        </el-row>
      </el-form>
    </template>
  </el-drawer>
</template>
<script setup>
import useOtherData from '@/hooks/useOtherData'
const { route, form, queryData, formName, idName,
  chooseFormProvince, chooseFormCity, chooseFormCounty,
  addRef, confirmAndSave, addDrawer, confirmClick, checkIdValid, openDrawer, closeDrawer, drawerLoading
} = useOtherData()

const { provinceData, cityData, areaData } = toRefs(queryData)

const formRules = reactive({
  provinceId: [{ required: true, message: '请选择省！', trigger: 'change' }],
  cityId: [{ required: true, message: '请选择市！', trigger: 'change' }],
  countyId: [{ required: true, message: '请选择区/县！', trigger: 'change' }],
  id: [
    { required: true, message: '请输入记录编号!', trigger: 'blur' },
    { min: 17, max: 17, message: '长度必须为17位', trigger: 'blur' },
    { validator: checkIdValid, trigger: 'blur' }
  ],
  trenchId: [{ required: true, message: '请输入探槽编号!', trigger: 'blur' }],
  occurDateEst: [{ pattern: /^(([1-9]+\d*)|(0{1}))(.[0-9]{2})?$/, message: '请输入正确的两位小数！', trigger: 'blur' }],
  occurDateEr: [{ pattern: /^(([1-9]+\d*)|(0{1}))(.[0-9]{2})?$/, message: '请输入正确的两位小数！', trigger: 'blur' }],
  offsetEst: [{ pattern: /^(([1-9]+\d*)|(0{1}))(.[0-9]{2})?$/, message: '请输入正确的两位小数！', trigger: 'blur' }],
  coordination: [{ pattern: /^(([1-9]+\d*)|(0{1}))(.[0-9]{2})?$/, message: '请输入正确的两位小数！', trigger: 'blur' }]
})
</script>